#!/usr/bin/python3
"""
keil compile hex format
|--------------------|------------------------|----------------------|--------------------|----------------|--------|-------|
|data length 1 byte *|* data address 2 bytes *|* data format 1 byte *|* payload 16 bytes *|* check 1 byte *|* 0x0d *|* 0x0a |
|--------------------|------------------------|----------------------|--------------------|----------------|--------|-------|
read back hex format
|--------------------|------------------------|----------------------|--------------------|----------------|--------|-------|
|data length 1 byte *|* data address 2 bytes *|* data format 1 byte *|* payload 32 bytes *|* check 1 byte *|* 0x0d *|* 0x0a |
|--------------------|------------------------|----------------------|--------------------|----------------|--------|-------|

020000040004F6  Extended Linear Address Record

00000001FF End of File Record
"""
import os
import sys

# Check parameter number 
if len(sys.argv) < 3:
    print("Please input two parameter.")
    sys.exit(1)

if len(sys.argv) > 3:
    print("only two hex comparisons are supported.")
    sys.exit(1)

# Get parameter
firstparam = sys.argv[1]
secondparam = sys.argv[2]

if os.path.exists(firstparam) == 0:
    print(f"{firstparam} not exit.")
    sys.exit(1)

if os.path.exists(secondparam) == 0:
    print(f"{secondparam} not exit.")
    sys.exit(1)

print(f"first hex: {firstparam}")
print(f"second hex: {secondparam}")

bytenum1 = 0
bytenum2 = 0
writebytenum1 = 0
writebytenum2 = 0
diffcount = 0

# Open hex
file1 = open(firstparam, "r")
file2 = open(secondparam, "r")
file3 = open(firstparam[:-3] + "txt", "w")
file4 = open(secondparam[:-3] + "txt", "w")

# Get payload and record
line1 = file1.readline()
line2 = file2.readline()

while (line1 != "") or (line2 != ""):
    if bytenum1 == 0:
        line1 = file1.readline()
        writebytenum1 = 1
        if (line1 != ""):
            bytenum1 = int(line1[1:3], 16) // 16

    if bytenum2 == 0:
        line2 = file2.readline()
        writebytenum2 = 1
        if (line2 != ""):
            bytenum2 = int(line2[1:3], 16) // 16

    if bytenum1 != 0:
        file3.write(str(line1[9 + (writebytenum1 - 1) * 32:9 + writebytenum1 * 32]) + "\r")
    else:
        file3.write(str(line1[1:]))

    if bytenum2 != 0:
        file4.write(str(line2[9 + (writebytenum2 - 1) * 32:9 + writebytenum2 * 32]) + "\r")
    else:
        file4.write(str(line2[1:]))

    if bytenum1 >= 1:
        bytenum1 -=1
        writebytenum1 +=1

    if bytenum2 >= 1:
        bytenum2 -=1
        writebytenum2 +=1

    if line1[9 + (writebytenum1 - 1) * 32:9 + writebytenum1 * 32] != line2[9 + (writebytenum2 - 1) * 32:9 + writebytenum2 * 32]:
        diffcount += 1

if diffcount == 0:
    print("\033[32m hex is the same \033[0m")
else:
    print("\033[31m hex is different \033[0m")

# Close file
file1.close()
file2.close()
file3.close()
file4.close()
